/**
 * Copyright (c) 2020 lizebang
 *
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

package gitutil

import (
	"fmt"

	"gitee.com/banggua/ggtsync/cmdutil"
)

func Init(root string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"init"}, args...)...)
}

func Config(root string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"config"}, args...)...)
}

func Add(root string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"add"}, args...)...)
}

func Commit(root, message string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"commit", "--message", message}, args...)...)
}

func Clone(root, remote string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"clone", remote}, args...)...)
}

func CloneBare(root, remote string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"clone", "--bare", remote}, args...)...)
}

func CloneMirror(root, remote string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"clone", "--mirror", remote}, args...)...)
}

func FetchAll(root, remote string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"fetch", remote, "+*:*", "--prune", "--force"}, args...)...)
}

func GetBranches(root string, args ...string) ([]string, error) {
	return cmdutil.RunOutput(root, "git", append([]string{"branch", "--list", `--format="%(refname)"`}, args...)...)
}

func GetShortBranches(root string, args ...string) ([]string, error) {
	return cmdutil.RunOutput(root, "git", append([]string{"branch", "--list", `--format="%(refname:short)"`}, args...)...)
}

func GetTags(root string, args ...string) ([]string, error) {
	return cmdutil.RunOutput(root, "git", append([]string{"tag", "--list", `--format="%(refname)"`}, args...)...)
}

func GetShortTags(root string, args ...string) ([]string, error) {
	return cmdutil.RunOutput(root, "git", append([]string{"tag", "--list", `--format="%(refname:short)"`}, args...)...)
}

func GetAllHashs(root string, args ...string) ([]string, error) {
	return cmdutil.RunOutput(root, "git", append([]string{"log", "--reverse", "--all", `--format=%H`}, args...)...)
}

func GetRefHash(root, ref string, args ...string) (string, error) {
	out, err := cmdutil.RunOutputOne(root, "git", append([]string{"log", ref, "-n1", `--format=%H`}, args...)...)
	if err != nil {
		out, err = cmdutil.RunOutputOne(root, "git", append([]string{"log", "--", ref, "-n1", `--format=%H`}, args...)...)
	}
	return out, nil
}

func GetRefHashs(root, ref string, args ...string) ([]string, error) {
	out, err := cmdutil.RunOutput(root, "git", append([]string{"log", ref, "--reverse", `--format=%H`}, args...)...)
	if err != nil {
		out, err = cmdutil.RunOutput(root, "git", append([]string{"log", "--", ref, "--reverse", `--format=%H`}, args...)...)
	}
	return out, err
}

func PushMirror(root, remote string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"push", "--mirror", remote}, args...)...)
}

func PushRef(root, remote, hash, ref string, args ...string) error {
	return cmdutil.Run(root, "git", append([]string{"push", remote, fmt.Sprintf("%s:%s", hash, ref)}, args...)...)
}
